<?php
//copie des fichiers
include($project_path.'/install_report.php5');
$error = false;
if(!isset($_SESSION['install']['package']))
{
	echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_NOVERSIONS»");';
	$error = true;
}
else
{
	$temp_path = $project_path.'/temp/';
	$file = $_SESSION['install']['package']['name'].'.'.str_replace('.', '_', $_SESSION['install']['package']['version']).'.update.tgz';
	if(!file_exists($temp_path.$file) || filesize($temp_path.$file)!=$_SESSION['install']['package']['size'] || md5_file($temp_path.$file)!=$_SESSION['install']['package']['md5'])
	{
		echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_DOWNLOADING»");';
		$error = true;
	}
}
if(!$error)
{
	include_once($project_path.'/class/ext/archive/gzip.class.php5');
	$package = $_SESSION['install']['package'];
	
	session_write_close();
	
	$setup = @new SimpleXMLElement($package['index']);
	$file = $package['name'].'.'.str_replace('.', '_', $package['version']).'.update.tgz';
	$tgz = new gzip_file($temp_path.$file);
		
	if(isset($_SESSION['install']['extract']['memo_list']))
	{
		$list_index = $_SESSION['install']['extract']['memo_list'];
	}
	else
	{
		$list_index = 0;
	}
	
	$nb = count($setup->screens[0]->extractfile);
	for($i=$list_index; $i<$nb; $i++)
	{
		$extract = $setup->screens[0]->extractfile[$i];
		if($_SERVER['HTTP_HOST'] == 'tt')
		{
			$tgz->set_options(array('basedir' => $project_path.'/temp/test/'.$extract['serverpath'], 'overwrite' => 1));
		}
		else
		{
			$tgz->set_options(array('basedir' => $project_path.'/'.$extract['serverpath'], 'overwrite' => 1));
		}
		$extract_limit = array(($i*1000)/$nb, (($i+1)*1000)/$nb);
		session_start();
		$_SESSION['install']['extract']['memo_list'] = $i;
		if(isset($_SESSION['install']['extract']['memo']))
		{
			$start = $_SESSION['install']['extract']['memo'];
			unset($_SESSION['install']['extract']['memo']);
			session_write_close();
		
			$result = $tgz->extract_files(array(array($extract['tarpath'], '*')), 'tar_hook', $start);
		}
		else
		{
			session_write_close();
			$result = $tgz->extract_files(array(array($extract['tarpath'], '*')), 'tar_hook');
		}
		if($result === false)
		{
			echo 'updatePerform("'.$_SESSION['install']['id'].'");';
			die();
		}
		else 
		{
			list($n_errors, $n_succes) = $result;
			if(count($n_errors)>0)
			{
				echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_EXTRACTING»");';
				$error = true;
				break;
			}
		}
	}
}
if(!$error)
{
	if(count($setup->deleted)>0)
	{
		foreach ($setup->deleted[0]->path as $path)
		{
			if(is_dir($project_path.$path))
			{
				@rmdir($project_path.$path);
			}
			elseif(is_file($project_path.$path))
			{
				@unlink($project_path.$path);
			}
		}
	}
	session_start();
	$next_screen = array_shift($_SESSION['install']['screens']);
	$_SESSION['install']['screen'] = $next_screen;
	$_SESSION['install']['progress'] = 0;
	$_SESSION['install']['status'] = '';
	echo 'updateUpdateStepCurrent("'.$_SESSION['install']['package']['name'].'", "'.$_SESSION['install']['package']['version'].'", '.$next_screen.')
updatePerform("'.$_SESSION['install']['id'].'", "«!ADMIN_INSTALL_'.$install_pages[$next_screen].'_HEAD»", "«!ADMIN_INSTALL_'.$install_pages[$next_screen].'_STATUS»");';
	if(in_array($next_screen, array(5,6)))
	{
		echo 'updateStartProgress()';
	}
}

function tar_hook($position, $archive_size, $extracting_file, $errors, $succes)
{
	global $start_time, $extract_limit, $last_progres;
	if((microtime(true) - $start_time)>23)
	{
		session_start();
		$_SESSION['install']['extract']['memo'] = array($position, $errors, $succes);
		session_write_close();
		return false;
	}
	if((microtime(true)-$last_progres)*1000>470)
	{
		session_start();
		//on diminue archivesize de la taille des fichiers de setup (environ 50Ko octets)
		$_SESSION['install']['progress'] = round($extract_limit[0] + (($position*($extract_limit[1]-$extract_limit[0]))/($archive_size-50000)));
		$_SESSION['install']['status'] = '«!ADMIN_INSTALL_FILE_STATUS» '.$extracting_file;
		if($_SESSION['install']['cancel'])
		{
			return false;
		}
		session_write_close();
		$last_progres= microtime(true);
	}
	return true;
}
?>